const { ccclass, property } = cc._decorator;

@ccclass
export default class ColorUtil {
    static hexToRgba(t) {
        return ColorUtil.isHex(t) ? {
            r: parseInt(t.substr(1, 2), 16) || 0,
            g: parseInt(t.substr(3, 2), 16) || 0,
            b: parseInt(t.substr(5, 2), 16) || 0,
            a: parseInt(t.substr(7, 2), 16) || 255
        } : null;
    }

    static rgbaToHex(e) {
        var t = (256 | e.r).toString(16).slice(1),
            a = (256 | e.g).toString(16).slice(1),
            o = (256 | e.b).toString(16).slice(1);
        return null == e.a ? ("#" + t + a + o).toUpperCase() : ("#" + t + a + o + (256 | e.a).toString(16).slice(1)).toUpperCase();
    }

    static isHex(e) {
        return /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6}|[0-9a-fA-f]{8})$/.test(e);
    }
}
